# _*_ coding : utf-8 _*_
# @Time : 2023/3/14 0014 17:15
# @Author : 菜鸟王小二
# @File : 3_urllib_请求对象的定制UA
# @Project : python爬虫
import urllib.request

# 由于服务器识别不了你是不是浏览器，所以需要添加user-agent
url = 'https://www.baidu.com'
headers = {
    'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.63'
}
# 使用Request添加headers
request = urllib.request.Request(url=url,headers=headers)

response = urllib.request.urlopen(request)

text = response.read().decode('utf-8')

print(text)